import { getCatitems, getFloordata, getSwiper } from "../../request/home";
import { observer } from "../../utils/observer";

interface Swiper {
  goods_id: number;
  image_src: string;
  navigator_url: string;
  open_type: string;
}
interface Catitems {
  image_src: string;
  name: string;
  navigator_url: string;
  open_type: string;
}
interface Childen {
  image_src: string;
  image_width: string | number;
  name: string;
  navigator_url: string;
  open_type: string;
}
interface Floordata {
  floor_title: {
    image_src: string;
    name: string;
  };
  side?: Childen;
  product_list: Array<Childen>;
}

let imgs: Array<Swiper> = [];
let catitems: Array<Catitems> = [];
let floordata: Array<Floordata> = [];
let count = 0;
let data = {
  imgs,
  count,
  floordata,
  catitems,
};

Page({
  data,
  onLoad() {
    data = observer(this.data, this);
    this.initSwiper();
    this.initCatitems();
    this.initFloordata();
  },
  async initSwiper() {
    const res = await getSwiper();
    data.imgs = res;
  },
  async initCatitems() {
    const res = await getCatitems();
    data.catitems = res;
  },
  async initFloordata() {
    const result = (await getFloordata()) as Array<Floordata>;
    result.forEach((item) => {
      item.side = item.product_list[0];
      item.product_list = item.product_list.filter((_, index) => index !== 0);
    });
    data.floordata = result;
  },
});
